
const S_URL = "http://tv.tvata.com:10248/";
const GETINFO_URL = S_URL + "index.php/api/getinfo/";
const ACT_URL = S_URL + "index.php/api/licensegetreg?device=";


function SimpleDeviceInfo() {
	this.auth_url = GETINFO_URL ; 
	this.id = null;
	this.getinfo = function(  ) {
		this.getID();

		if ( this.id == null ) {
			return null;
		}

		var info_url = this.auth_url + this.id;
		var hc = wae.create("Net.HttpClient");
		var res = hc.getWithSync( info_url);
		// alert( res + "\n" + info_url );
		try {
			var dinfo = JSON.parse(res);
			this.info = dinfo;
			return dinfo;		
		}catch(e){}
		return null;
	}

	this.activate = function( code, contact, tel ) {
	    var aurl = ACT_URL + this.id 
	                + "&license=" + code + "&contacts="+ contact+"&tel=" + tel;
	    if( _debug ) console.log("activate url: " + aurl );

		var hc = wae.create("Net.HttpClient");
		var resstr = hc.getWithSync( aurl );
		var res = null;
		try {
			res = JSON.parse(resstr);
		}catch(e){}
		if ( ! res ) return "get data not valid data " + resstr ;
		// if ( res.msg != "ok" ) return "server return: " + res.msg;
		if ( res.msg == "ok" ) 
			return "OK";

		return res.msg;
	}


	this.getID = function() {
		if ( this.id != null )
			return this.id;
		var id = localStorage.getItem("DeviceInfo_RID");
		if ( !!id ) {
			this.id = id;
			return this.id;
		}
		if ( typeof( this.device_obj ) == "undefined" ) {
			try {
				this.device_obj = wae.create("Devices");
			}catch(e){}
			if ( typeof( this.device_obj ) != "undefined" ) {
				this.id = this.device_obj.getDeviceID();
				localStorage.setItem("DeviceInfo_RID", this.id);
				return this.id;
			}
		}

		if ( typeof(iwae) == "undefined" ) {
			// running on browser
			var id = "R";
			for(var i=0;i<12;i++) 
				id+= Math.ceil(Math.random()*16).toString(16);
			localStorage.setItem("DeviceInfo_RID", id);
			this.id = id;
			return id;
		}
		return null;
	}
}
